package flc.ast.activity;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.CityJsonBean;
import flc.ast.bean.ResumeBean;
import flc.ast.databinding.ActivityBasicInfoBinding;
import flc.ast.dialog.BirthdayDialog;
import flc.ast.dialog.MaritalStatusDialog;
import flc.ast.dialog.PoliticalStatusDialog;
import flc.ast.dialog.SexDialog;
import flc.ast.dialog.TitleRenameDialog;
import flc.ast.utils.CityLevelsListDate;
import flc.ast.utils.ImageToBase64Util;
import flc.ast.utils.ResumeDataUtils;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.StkPermissionHelper;

public class BasicInfoActivity extends BaseAc<ActivityBasicInfoBinding> {

    public static int position;
    public static int modulePos;
    private ResumeBean resumeBean;
    private ResumeBean.Module module;
    private ResumeBean.BaseInfoData baseInfoData;
    private OptionsPickerView nativePlaceOptions;
    private CityLevelsListDate levelsListDate;
    private ArrayList<CityJsonBean> jsonBeans;
    private ArrayList<ArrayList<String>> arrayLists;

    @Override
    protected int onCreate() {
        return R.layout.activity_basic_info;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivBasicInfoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
        mDataBinding.tvTitle.setOnClickListener(this);
        mDataBinding.ivPhoto.setOnClickListener(this);
        mDataBinding.llSex.setOnClickListener(this);
        mDataBinding.llBirthday.setOnClickListener(this);
        mDataBinding.llMaritalStatus.setOnClickListener(this);
        mDataBinding.llPoliticalStatus.setOnClickListener(this);
        mDataBinding.llNativePlace.setOnClickListener(this);

    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSave://保存
                save();
                break;
            case R.id.tvTitle://标题
                showRenameDialog();
                break;
            case R.id.ivPhoto://照片
                getPermission();
                break;
            case R.id.llSex://性别
                showSexDialog();
                break;
            case R.id.llBirthday://生日
                showBirthdayDialog();
                break;
            case R.id.llMaritalStatus://婚姻状态
                showMaritalStatusDialog();
                break;
            case R.id.llPoliticalStatus://政治面貌
                showPoliticalStatusDialog();
                break;
            case R.id.llNativePlace://籍贯
                if (KeyboardUtils.isSoftInputVisible(this)) {
                    KeyboardUtils.hideSoftInput(this);
                }
                if (nativePlaceOptions != null) {
                    nativePlaceOptions.show();
                }
                break;
        }
    }

    @Override
    protected void initData() {
        levelsListDate = new CityLevelsListDate(mContext);
        jsonBeans = levelsListDate.initJsonData("citys_data.json");
        arrayLists = levelsListDate.initJsonData1("citys_data.json");
        showNativePlaceView();
        getData();
    }


    private void getData() {
        resumeBean = ResumeDataUtils.getResumeList().get(position);
        module = resumeBean.getResumeData().get(modulePos);
        mDataBinding.tvTitle.setText(module.getAlias());
        if (module.getData() != null) {
            baseInfoData = GsonUtils.fromJson(GsonUtils.toJson(module.getData()), ResumeBean.BaseInfoData.class);
            if (baseInfoData.getAvatar() != null) {
                Glide.with(mContext).load(baseInfoData.getAvatar()).into(mDataBinding.ivPhoto);
            }
            mDataBinding.etName.setText(baseInfoData.getName());
            mDataBinding.etPhone.setText(baseInfoData.getPhone());
            mDataBinding.etEmail.setText(baseInfoData.getEmail());
            mDataBinding.tvSex.setText(baseInfoData.getSex());
            mDataBinding.tvBirthday.setText(baseInfoData.getDateOfBirth());
            mDataBinding.etHeight.setText(baseInfoData.getHeight());
            mDataBinding.etWeight.setText(baseInfoData.getWeight());
            mDataBinding.tvMaritalStatus.setText(baseInfoData.getMaritalStatus());
            mDataBinding.tvPoliticalStatus.setText(baseInfoData.getPoliticalStatus());
            mDataBinding.etNation.setText(baseInfoData.getNation());
            mDataBinding.tvNativePlace.setText(baseInfoData.getNativePlace());
        } else {
            baseInfoData = new ResumeBean.BaseInfoData();
        }
    }

    private void save() {
        String name = mDataBinding.etName.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showShort(getString(R.string.please_input_people_name));
            return;
        }
        String phone = mDataBinding.etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort(getString(R.string.please_input_phone_number));
            return;
        }
        String email = mDataBinding.etEmail.getText().toString().trim();
        if (TextUtils.isEmpty(email)) {
            ToastUtils.showShort(getString(R.string.please_input_email));
            return;
        }
        String sex = mDataBinding.tvSex.getText().toString().trim();
        if (TextUtils.isEmpty(sex)) {
            ToastUtils.showShort(getString(R.string.please_sel_sex));
            return;
        }
        String birthday = mDataBinding.tvBirthday.getText().toString().trim();
        if (TextUtils.isEmpty(birthday)) {
            ToastUtils.showShort(getString(R.string.please_sel_birthday));
            return;
        }
        String height = mDataBinding.etHeight.getText().toString().trim();
        if (TextUtils.isEmpty(height)) {
            ToastUtils.showShort(getString(R.string.please_input_height));
            return;
        }
        String weight = mDataBinding.etWeight.getText().toString().trim();
        if (TextUtils.isEmpty(weight)) {
            ToastUtils.showShort(getString(R.string.please_input_weight));
            return;
        }
        String maritalStatus = mDataBinding.tvMaritalStatus.getText().toString().trim();
        if (TextUtils.isEmpty(maritalStatus)) {
            ToastUtils.showShort(getString(R.string.please_sel_marital_status));
            return;
        }
        String politicalStatus = mDataBinding.tvPoliticalStatus.getText().toString().trim();
        if (TextUtils.isEmpty(politicalStatus)) {
            ToastUtils.showShort(getString(R.string.please_sel_political_status));
            return;
        }
        String nation = mDataBinding.etNation.getText().toString().trim();
        if (TextUtils.isEmpty(nation)) {
            ToastUtils.showShort(getString(R.string.please_input_nation));
            return;
        }
        String nativePlace = mDataBinding.tvNativePlace.getText().toString().trim();
        if (TextUtils.isEmpty(nativePlace)) {
            ToastUtils.showShort(getString(R.string.please_sel_native_place));
            return;
        }
        baseInfoData.setName(name);
        baseInfoData.setPhone(phone);
        baseInfoData.setEmail(email);
        baseInfoData.setSex(sex);
        baseInfoData.setDateOfBirth(birthday);
        baseInfoData.setHeight(height);
        baseInfoData.setWeight(weight);
        baseInfoData.setMaritalStatus(maritalStatus);
        baseInfoData.setPoliticalStatus(politicalStatus);
        baseInfoData.setNation(nation);
        baseInfoData.setNativePlace(nativePlace);
        baseInfoData.setBorth2Age(false);
        baseInfoData.setShowAvatar(baseInfoData.getAvatar() == null ? false : true);

        List<ResumeBean> listSave = ResumeDataUtils.getResumeList();
        module.setData(baseInfoData);
        listSave.set(position, resumeBean);
        ResumeDataUtils.saveResumeList(listSave);
        ToastUtils.showShort(getString(R.string.save_success));
        finish();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getImage();
                    }
                }).request();
    }

    private void getImage() {
        PhotoSelectorBuilder.builder(this)
                .mode(PhotoConfig.Mode.PHOTO)
                .minCount(1)
                .maxCount(1)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        ArrayList<String> pathList = new ArrayList<>();
                        for (SelectMediaEntity entity : data) {
                            pathList.add(entity.getTargetPath());
                        }
                        try {
                            baseInfoData.setAvatar(ImageToBase64Util.convertToBase64WithPrefix(pathList.get(0),500));
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                        Glide.with(mContext).load(baseInfoData.getAvatar()).into(mDataBinding.ivPhoto);
                    }
                });
    }

    private void showRenameDialog() {
        TitleRenameDialog renameDialog = new TitleRenameDialog(mContext);
        renameDialog.text = mDataBinding.tvTitle.getText().toString().trim();
        renameDialog.setListener(new TitleRenameDialog.Ilistener() {

            @Override
            public void clickRename(String name) {
                module.setAlias(name);
                mDataBinding.tvTitle.setText(name);
            }
        });
        renameDialog.show();
    }

    private void showSexDialog() {
        SexDialog sexDialog = new SexDialog(mContext);
        sexDialog.setListener(new SexDialog.Ilistener() {

            @Override
            public void clickConfirm(String sex) {
                mDataBinding.tvSex.setText(sex);
            }
        });
        sexDialog.show();
    }

    private void showBirthdayDialog() {
        BirthdayDialog birthdayDialog = new BirthdayDialog(mContext);
        birthdayDialog.setListener(new BirthdayDialog.Ilistener() {

            @Override
            public void clickConfirm(String date) {
                mDataBinding.tvBirthday.setText(date);
            }
        });
        birthdayDialog.show();
    }

    private void showMaritalStatusDialog() {
        MaritalStatusDialog maritalStatusDialog = new MaritalStatusDialog(mContext);
        maritalStatusDialog.setListener(new MaritalStatusDialog.Ilistener() {

            @Override
            public void clickConfirm(String maritalStatus) {
                mDataBinding.tvMaritalStatus.setText(maritalStatus);
            }
        });
        maritalStatusDialog.show();
    }

    private void showPoliticalStatusDialog() {
        PoliticalStatusDialog politicalStatusDialog = new PoliticalStatusDialog(mContext);
        politicalStatusDialog.setListener(new PoliticalStatusDialog.Ilistener() {

            @Override
            public void clickConfirm(String politicalStatus) {
                mDataBinding.tvPoliticalStatus.setText(politicalStatus);
            }
        });
        politicalStatusDialog.show();
    }

    private void showNativePlaceView() {
        //条件选择器
        nativePlaceOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                try {
                    mDataBinding.tvNativePlace.setText(jsonBeans.get(options1).getREGION_NAME() +
                            jsonBeans.get(options1).getCity().get(options2).getREGION_NAME());
                } catch (Exception e) {
                    mDataBinding.tvNativePlace.setText(jsonBeans.get(options1).getREGION_NAME() +
                            jsonBeans.get(options1).getCity().get(options2).getREGION_NAME());
                }
            }
        }).setTitleText(getString(R.string.native_place))//标题
                .setSubCalSize(16)//确定和取消文字大小
                .setTitleSize(14)//标题文字大小
                .setTitleColor(Color.parseColor("#000000"))//标题文字颜色
                .setSubmitColor(Color.parseColor("#2262D0"))//确定按钮文字颜色
                .setCancelColor(Color.parseColor("#A4A4A4"))//取消按钮文字颜色
                .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                .setContentTextSize(16)//滚轮文字大小
                .build();
        nativePlaceOptions.setPicker(jsonBeans, arrayLists);
    }

}